package com.fcl.oschina.model;

import com.fcl.mylibrary.http.HttpClient;
import com.fcl.mylibrary.util.FLog;
import com.fcl.mylibrary.util.StringUtil;
import com.fcl.oschina.Constant;
import com.fcl.oschina.entity.BaseReturn;
import com.fcl.oschina.entity.ListResult;
import com.fcl.oschina.entity.NewItem;
import com.fcl.oschina.ui.fragment.BlogView;
import com.fcl.oschina.ui.fragment.NewsView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.fcl.oschina.util.HttpUtil.getResult;

/**
 * Created by Administrator on 2017/3/20.
 */

public class BlogModel {

    private String TAG = "BlogModel";
    private BlogView mBlogView;
    private String mNextPage = "";

    public BlogModel(BlogView blogView) {
        this.mBlogView = blogView;
    }

    public void setNextPage(String mNextPage) {
        this.mNextPage = mNextPage;
    }

    public void getBlogList(final int catalog) {
//        if (!NetWorkUtils.isConnectedByState(AppContext.getInstance())) {
//            mNewsView.noNet();
//            return;
//        }

        String url = Constant.BASE_URL + "action/apiv2/blog?pageToken=" + mNextPage + "&catalog=" + catalog;
        HttpClient client = new HttpClient(url, new HttpClient.ResultListener() {
            @Override
            public void dealResult(String result) {
                Gson gson = new Gson();
                BaseReturn baseReturn = gson.fromJson(result, new TypeToken<BaseReturn<ListResult<NewItem>>>() {
                }.getType());
                if (baseReturn.getCode() == 1) {
                    FLog.error(TAG, "success thread" + Thread.currentThread().getId());
                    ListResult<NewItem> data = (ListResult<NewItem>) baseReturn.getResult();

                    mBlogView.setData(data.getItems(), !StringUtil.isEmpty(mNextPage));
                    mNextPage = data.getNextPageToken();
                }
            }
        });
        client.get();


    }


    public void getUserBlogList(final int authorid) {
//        if (!NetWorkUtils.isConnectedByState(AppContext.getInstance())) {
//            mNewsView.noNet();
//            return;
//        }

        String url = Constant.BASE_URL + "action/apiv2/blog?pageToken=" + mNextPage + "&authorId=" + authorid;
        HttpClient client = new HttpClient(url, new HttpClient.ResultListener() {
            @Override
            public void dealResult(String result) {
                Gson gson = new Gson();
                BaseReturn baseReturn = gson.fromJson(result, new TypeToken<BaseReturn<ListResult<NewItem>>>() {
                }.getType());
                if (baseReturn.getCode() == 1) {
                    FLog.error(TAG, "success thread" + Thread.currentThread().getId());
                    ListResult<NewItem> data = (ListResult<NewItem>) baseReturn.getResult();

                    mBlogView.setData(data.getItems(), !StringUtil.isEmpty(mNextPage));
                    mNextPage = data.getNextPageToken();
                }
            }
        });
        client.get();

    }

}
